package com.example.aijiancece.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.aijiancece.AboutActivity;
import com.example.aijiancece.LoginActivity;
import com.example.aijiancece.R;
import com.example.aijiancece.UserManager;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class ProfileFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_profile, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        MaterialCardView cardAbout = view.findViewById(R.id.cardAbout);
        MaterialCardView cardFeedback = view.findViewById(R.id.cardFeedback);
        MaterialCardView cardPrivacy = view.findViewById(R.id.cardPrivacy);
        MaterialCardView cardTerms = view.findViewById(R.id.cardTerms);

        cardAbout.setOnClickListener(v -> startActivity(new Intent(requireContext(), AboutActivity.class)));

        cardFeedback.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:support@example.com"));
            intent.putExtra(Intent.EXTRA_SUBJECT, "AI内容检测 - 用户反馈");
            startActivity(Intent.createChooser(intent, "发送邮件"));
        });

        cardPrivacy.setOnClickListener(v -> {
            // TODO: 跳转到隐私政策页面
        });

        cardTerms.setOnClickListener(v -> {
            // TODO: 跳转到使用条款页面
        });

        MaterialButton btnLogout = view.findViewById(R.id.btnLogout);
        btnLogout.setOnClickListener(v -> showLogoutDialog());
    }

    private void showLogoutDialog() {
        new MaterialAlertDialogBuilder(requireContext())
            .setTitle("退出登录")
            .setMessage("确定要退出登录吗？")
            .setNegativeButton("取消", null)
            .setPositiveButton("确定", (dialog, which) -> {
                // 清除用户登录状态
                UserManager.getInstance(requireContext()).logout();
                
                // 跳转到登录页面
                Intent intent = new Intent(requireContext(), LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                requireActivity().finish();
            })
            .show();
    }
} 